package cn.fl.audit.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.audit.condition.AudInterviewInfoQueryCondition;
import cn.fl.audit.service.IAudCreditInfoService;
import cn.fl.audit.service.IAudInterviewInfoService;
import cn.fl.audit.service.IAudReturnVisitService;
import cn.fl.audit.vo.AudInterviewInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;

/**
 * @Description: 面谈信息表 控制层
 */
@Controller
@RequestMapping("/audit/audInterviewInfo")
public class AudInterviewInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AudInterviewInfoController.class);

	/**
	 * AudInterviewInfo 服务层BEAN
	 */
	@Autowired
	private IAudInterviewInfoService audInterviewInfoService;
	
	@Autowired
	private IAudReturnVisitService audReturnVisitService;

	@Autowired
	private IAudCreditInfoService audCreditInfoService;
	
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("audit/audInterviewInfo/list");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到新增/修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		String projectId = request.getParameter("projectId");
		String isDetail = request.getParameter("isDetail");
		ModelAndView view = new ModelAndView("audit/audInterviewInfo/audInterviewInfoAdd");
		view.addObject("projectId", projectId);
		view.addObject("isDetail", isDetail);
		
		//运营年限
		List<KeyValueVO> operateTmCdList = this.audReturnVisitService.searchBizExpList();
		view.addObject("operateTmCdList", operateTmCdList);
		view.addObject("localLiveTmCdList", operateTmCdList);
		//主货物大类
		List<KeyValueVO> mainGdsTypCdList = this.audCreditInfoService.searchGoodsTypListByLvl("1");
		view.addObject("mainGdsTypCdList", mainGdsTypCdList);
		//货物来源
		List<KeyValueVO> gdsResCdList = this.audReturnVisitService.searchGoodsResList();
		view.addObject("gdsResCdList", gdsResCdList);
		//主货物结算方式
		List<KeyValueVO> stlWayCdList = this.audReturnVisitService.searchGdsStlWayCdList();
		view.addObject("stlWayCdList", stlWayCdList);
		//司机情况
		List<KeyValueVO> driverCdList = this.audReturnVisitService.searchDriverList();
		view.addObject("driverCdList", driverCdList);
		//第二还款来源
		List<KeyValueVO> secondPayResCdList = this.audReturnVisitService.searchSecondPayResCdList();
		view.addObject("secondPayResCdList", secondPayResCdList);
		//客户经理评级
		List<KeyValueVO> cstMgrLvlCdList = this.audInterviewInfoService.searchCstMgrLvlCdList();
		view.addObject("cstMgrLvlCdList", cstMgrLvlCdList);
		//资产状况
		List<KeyValueVO> assetStatusCdList = this.audInterviewInfoService.searchAssetStatusCdList();
		view.addObject("assetStatusCdList", assetStatusCdList);
		//婚姻状况
		List<KeyValueVO> marrStatusCdList = this.audInterviewInfoService.searchMarrStatusCdList();
		view.addObject("marrStatusCdList", marrStatusCdList);
		//首付款来源
		List<KeyValueVO> firstPayResCdList = this.audInterviewInfoService.searchFirstPayResCdList();
		view.addObject("firstPayResCdList", firstPayResCdList);
		//风控评级
		List<KeyValueVO> riskCtrlLvlCdList = this.audInterviewInfoService.searchRiskCtrlLvlCdList();
		view.addObject("riskCtrlLvlCdList", riskCtrlLvlCdList);
		
		
		BaseLogUtils.info(logger, "toAddPage", "跳转到修改页面.[projectId="+projectId+",isDetail"+isDetail+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		AudInterviewInfoQueryCondition qc = new AudInterviewInfoQueryCondition();
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();//this.audInterviewInfoService.searchListPage(page);
		List<AudInterviewInfoVO> list = (List<AudInterviewInfoVO>) pager.getResultList();
		ListPageVO<AudInterviewInfoVO> listPage = new ListPageVO<AudInterviewInfoVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.audInterviewInfoService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getByPrjId.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getByPrjId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String prjId = request.getParameter("projectId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(prjId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getByPrjId", "获取数据,主键不存在.[prjId="+prjId+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			AudInterviewInfoVO vo = this.audInterviewInfoService.selectByPrjId(Long.valueOf(prjId));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getByPrjId", "获取数据,数据不存在.[prjId="+prjId+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getByPrjId", "获取数据.[prjId="+prjId+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getByPrjId", "获取失败.[prjId="+prjId+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getByPrjId", "获取失败.[prjId="+prjId+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		AudInterviewInfoVO entity = null;
		try {
			entity = this.getRequestParamForAdd(request);
			Long id = this.audInterviewInfoService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		AudInterviewInfoVO entity = null;
		try {
			entity = this.getRequestParamForModify(request);
			entity.setId(Long.valueOf(id));
			boolean success = this.audInterviewInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	//********************私有方法********************//
	/**
	 * 获取参数信息
	 * @param request
	 * @return
	 * @throws BaseException
	 */
	private AudInterviewInfoVO getRequestParam(HttpServletRequest request)throws BaseException{
		String projectId = request.getParameter("projectId");//项目ID
		String proIntId = request.getParameter("proIntId");//流程实例ID
		String cstMgrLvlCd = request.getParameter("cstMgrLvlCd");//客户经理评级
		String operateTmCd = request.getParameter("operateTmCd");//运营年限
		String localLiveTmCd = request.getParameter("localLiveTmCd");//本地生活年限
		String secondPayResCd = request.getParameter("secondPayResCd");//第二还款来源
		String assetStatusCd = request.getParameter("assetStatusCd");//资产状况
		String driverCd = request.getParameter("driverCd");//司机情况
		String marrStatusCd = request.getParameter("marrStatusCd");//婚姻状况
		String gdsResCd = request.getParameter("gdsResCd");//运力来源
		String operateLine = request.getParameter("operateLine");//运营路线
		String firstPayResCd = request.getParameter("firstPayResCd");//首付款来源
		String br = request.getParameter("br");//车辆品牌
		String stlWayCd = request.getParameter("stlWayCd");//结算方式
		String mainGdsTypCd = request.getParameter("mainGdsTypCd");//主货物大类
		String gdsTypCd = request.getParameter("gdsTypCd");//主运输货物
		String riskCtrlLvlCd = request.getParameter("riskCtrlLvlCd");//风控评级
		
		AudInterviewInfoVO audInterviewInfoVO = new AudInterviewInfoVO();
		
		if(StringUtils.isNotEmpty(projectId)){
			audInterviewInfoVO.setProjectId(Long.valueOf(projectId));
		}
		audInterviewInfoVO.setProIntId(proIntId);
		audInterviewInfoVO.setCstMgrLvlCd(cstMgrLvlCd);
		audInterviewInfoVO.setOperateTmCd(operateTmCd);
		audInterviewInfoVO.setLocalLiveTmCd(localLiveTmCd);
		audInterviewInfoVO.setSecondPayResCd(secondPayResCd);
		audInterviewInfoVO.setAssetStatusCd(assetStatusCd);
		audInterviewInfoVO.setDriverCd(driverCd);
		audInterviewInfoVO.setMarrStatusCd(marrStatusCd);
		audInterviewInfoVO.setGdsResCd(gdsResCd);
		audInterviewInfoVO.setOperateLine(operateLine);
		audInterviewInfoVO.setFirstPayResCd(firstPayResCd);
		audInterviewInfoVO.setBr(br);
		audInterviewInfoVO.setStlWayCd(stlWayCd);
		audInterviewInfoVO.setMainGdsTypCd(mainGdsTypCd);
		audInterviewInfoVO.setGdsTypCd(gdsTypCd);
		audInterviewInfoVO.setRiskCtrlLvlCd(riskCtrlLvlCd);
		
		return audInterviewInfoVO;
	}
	/**
	 * 获取请求参数(新增)
	 * @param request 请求
	 * @return 
	 */
	private AudInterviewInfoVO getRequestParamForAdd(HttpServletRequest request)throws BaseException{
		AudInterviewInfoVO audInterviewInfoVO = this.getRequestParam(request);
		audInterviewInfoVO.setCrtTm(new Date());
		audInterviewInfoVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		return audInterviewInfoVO;
	}
	
	/**
	 * 获取请求参数(修改)
	 * @param request 请求
	 * @return 
	 */
	private AudInterviewInfoVO getRequestParamForModify(HttpServletRequest request)throws BaseException{
		AudInterviewInfoVO audInterviewInfoVO = this.getRequestParam(request);
		audInterviewInfoVO.setMdfTm(new Date());
		audInterviewInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		return audInterviewInfoVO;
	}
}

